﻿using System.Windows;

namespace System.Windows
{
	public static class PointExtensions
	{
		public static Point Subtract(this Point p, Point value)
		{
			return p.Subtract(value.X, value.Y);
		}

		public static Point Subtract(this Point p, double x, double y)
		{
			return new Point(p.X - x, p.Y - y);
		}

		public static Point Add(this Point p, Point value)
		{
			return p.Add(value.X, value.Y);
		}

		public static Point Add(this Point p, double x, double y)
		{
			return new Point(p.X + x, p.Y + y);
		}

		public static double Distance(this Point p, Point value)
		{
			var num = p.X - value.X;
			var num1 = p.Y - value.Y;
			var num3 = (num1 * num1) + (num * num);
			return Math.Sqrt(num3);
		}

		public static Point Rotate(this Point origin, Point value, double angle)
		{
			double radians = ToRadians(angle);

			double sinT = Math.Sin(radians);
			double cosT = Math.Cos(radians);

			var translatedPoint = value.Subtract(origin);

			double x = translatedPoint.X * cosT - translatedPoint.Y * sinT;
			double y = translatedPoint.X * sinT + translatedPoint.Y * cosT;

			return new Point(x + origin.X, y + origin.Y);
		}

		public static double FindAngle(this Point origin, Point value)
		{
			return ToDegrees(Math.Atan2(value.X - origin.X, value.Y - origin.Y));
		}

		/// <summary>Converts radians to degrees.</summary>
		/// <param name="radians">The angle in radians.</param>
		public static double ToDegrees(double radians)
		{
			return (radians * 57.29578);
		}

		/// <summary>Converts degrees to radians.</summary>
		/// <param name="degrees">The angle in degrees.</param>
		public static double ToRadians(double degrees)
		{
			return (degrees * 0.01745329);
		}
	}
}
